﻿using MQTTnet;
using MQTTnet.Client;

namespace IotClientApp.Server.MqttClient
{
    public class MqttClientService : IMqttClientService
    {
        private readonly DeviceMQTTClient _mQTTClient;
        public MqttClientService(DeviceMQTTClient mQTTClient)
        {
            _mQTTClient = mQTTClient;
        }
        /// <summary>
        /// 订阅主题
        /// </summary>
        /// <param name="topicName"></param>
        /// <returns></returns>
        public async Task<int> SubscribeAsync(string topicName)
        {
            if (string.IsNullOrEmpty(topicName))
            {
                return -1;
            }
            if (!_mQTTClient.mqttClient.IsConnected)
            {
                return -2;
            }
            await _mQTTClient.mqttClient.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic(topicName).Build());
            return 1;
        }
        /// <summary>
        /// 取消订阅主题
        /// </summary>
        /// <param name="topicName"></param>
        /// <returns></returns>
        public async Task<int> UnSubscribeAsync(string topicName)
        {
            if (string.IsNullOrEmpty(topicName))
            {
                return -1;
            }
            if (!_mQTTClient.mqttClient.IsConnected)
            {
                return -2;
            }
            MqttClientUnsubscribeOptions mqttClientUnsubscribeOptions = new MqttClientUnsubscribeOptions()
            {
                TopicFilters = new List<string>() { topicName }
            };
            await _mQTTClient.mqttClient.UnsubscribeAsync(mqttClientUnsubscribeOptions);
            return 1;
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="topicName"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task<MqttClientPublishResult> PublishAsync(string topicName, string message)
        {
            if (string.IsNullOrEmpty(topicName))
            {
                return new MqttClientPublishResult();
            }
            if (!_mQTTClient.mqttClient.IsConnected)
            {
                return new MqttClientPublishResult();
            }
            var messagebuilder = new MqttApplicationMessageBuilder()
                .WithTopic(topicName)
                .WithPayload(message)
                .WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce)
                //.WithExactlyOnceQoS()
                //.WithAtLeastOnceQoS()                
                .WithRetainFlag(false)
                .Build();
            MqttClientPublishResult mqttClientPublishResult = await _mQTTClient.mqttClient.PublishAsync(messagebuilder);
            return mqttClientPublishResult;
        }
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public async Task ReceiveAsync(Action<MqttApplicationMessageReceivedEventArgs> action)
        {
            _mQTTClient.mqttClient.ApplicationMessageReceivedAsync += async e =>
            {
                action(e);
            };
        }
    }
}
